<?php

namespace app\model\system;

use albert\traits\ModelTrait;
use app\model\BaseModel;
use think\db\exception\DbException;

/**
 * 组合数据详情表 Model
 * Class SystemGroupData
 * @package app\models\system
 */
class SystemGroupData extends BaseModel
{
    use ModelTrait;

    /**
     * 初始化查询条件
     * Author: Albert3306
     * Date: 2021/7/7
     * @param $where
     * @param null $model
     * @return SystemGroupData|mixed
     */
    public static function initWhere($where, $model = null)
    {
        $model = $model ?? new self;
        if (isset($where['id']) && $where['id'] !== '') $model = $model->where('id', $where['id']);
        if (isset($where['gid']) && $where['gid'] !== '') $model = $model->where('gid', $where['gid']);
        if (isset($where['status']) && $where['status'] !== '') $model = $model->where('status', $where['status']);
        return $model;
    }

    /**
     * 根据where条件获取当前表中的前20条数据
     * Author: Albert3306
     * Date: 2021/7/7
     * @param $where
     * @return ModelTrait|BaseModel|array
     * @throws DbException
     */
    public static function getList($where)
    {
        $model = self::initWhere($where)->order('sort desc,id ASC');
        return self::page($model, function ($item) {
            $info = json_decode($item->value, true);
            foreach ($info as $index => $value) {
                if ($value['type'] == 'checkbox') $info[$index]['value'] = implode(',', $value['value']);
            }
            $item->value = $info;
        }, $where);
    }
}